import { IMenu } from '#/menu';
import { CacheEnum } from '@/enum/cacheEnum';
import router from '@/router';
import utils from '@/utils';
import {ref} from 'vue'
/* vue3 支持 组合式 compisition api 后， 可以定义全局的 响应式数据  */


class Menu { 
  public menus = ref<IMenu[]>([])
  public history = ref<IMenu[]>([])

  constructor() {
    this.menus.value = this.getMenuByRoute()
    /* 这里  */
    this.history.value = utils.store.get(CacheEnum.HISTORY_MENU) ?? [] 
  }
  getMenuByRoute() {
    return router
      .getRoutes()
      .filter(route => route.children.length && route.meta.menu)
      .map(route => {
        let menu: IMenu = { ...route.meta?.menu }
        menu.children = route.children
          .filter(route => route.meta?.menu)
          .map(route => {
            return { ...route.meta?.menu, route: route.name }
          }) as IMenu[]
        return menu
      })
      .filter(menu => menu.children?.length) as IMenu[]
  }
}

export default new Menu()